// index.js
Page({
  data: {
    bannerList: [
      "/image/banner/banner1.jpg",
      "/image/banner/banner2.jpg",
      "/image/banner/banner3.jpg"
    ],
    countdownHours: "15",
    countdownMinutes: "05",
    countdownSeconds: "40",
    flashSaleProducts: [
      {
        id: 1,
        image: "/image/banner/banner1.jpg",
        price: "388",
        originalPrice: "799"
      },
      {
        id: 2,
        image: "/image/banner/banner1.jpg",
        price: "388",
        originalPrice: "799"
      },
      {
        id: 3,
        image: "/image/banner/banner1.jpg",
        price: "388",
        originalPrice: "799"
      },
      {
        id: 4,
        image: "/image/banner/banner1.jpg",
        price: "388",
        originalPrice: "799"
      }
    ],
    newProducts: [
      {
        id: 1,
        image: "/image/banner/banner1.jpg",
        title: "精品萨摩耶幼犬 纯种萨摩耶 微笑天使 多只可挑选",
        price: "3800"
      },
      {
        id: 2,
        image: "/image/banner/banner2.jpg",
        title: "柯基幼犬 威尔士柯基犬 两色三色均有 包纯种健康",
        price: "4200"
      },
      {
        id: 3,
        image: "/image/banner/banner3.jpg",
        title: "布偶猫活体 幼猫纯种 可爱粘人 多色可选",
        price: "5500"
      },
      {
        id: 4,
        image: "/image/banner/banner1.jpg",
        title: "金毛寻回犬 忠诚友善 家庭伴侣犬 包健康纯种",
        price: "2800"
      },
      {
        id: 5,
        image: "/image/banner/banner2.jpg",
        title: "宠物猫咪 英国短毛猫 蓝猫 蓝白 立耳 折耳",
        price: "3000"
      },
      {
        id: 6,
        image: "/image/banner/banner3.jpg",
        title: "比熊犬 白色毛绒 不掉毛体味小 适合家养",
        price: "1900"
      }
    ]
  },
  onLoad: function() {
    // 初始化倒计时
    this.startCountdown();
  },
  
  // 功能模块点击事件
  onFunctionTap: function(e) {
    const type = e.currentTarget.dataset.type;
    
    switch(type) {
      case 'taxi':
        wx.showToast({
          title: '一键打车功能开发中',
          icon: 'none'
        });
        break;
      case 'adopt':
        wx.showToast({
          title: '领养宠物功能开发中',
          icon: 'none'
        });
        break;
      case 'insurance':
        wx.showToast({
          title: '宠物医保功能开发中',
          icon: 'none'
        });
        break;
      case 'record':
        wx.showToast({
          title: '建档立卡功能开发中',
          icon: 'none'
        });
        break;
    }
  },

  // 开始倒计时
  startCountdown: function() {
    // 设置结束时间为当天的23:59:59
    const now = new Date();
    const endTime = new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate(),
      23, 59, 59
    ).getTime();
    
    // 更新倒计时
    this.updateCountdown(endTime);
    
    // 创建定时器，每秒更新一次
    this.countdownTimer = setInterval(() => {
      this.updateCountdown(endTime);
    }, 1000);
  },
  
  // 更新倒计时
  updateCountdown: function(endTime) {
    const now = new Date().getTime();
    const diff = endTime - now;
    
    if (diff <= 0) {
      // 倒计时结束，重新设置明天的倒计时
      clearInterval(this.countdownTimer);
      const tomorrow = new Date();
      tomorrow.setDate(tomorrow.getDate() + 1);
      tomorrow.setHours(23, 59, 59, 0);
      this.updateCountdown(tomorrow.getTime());
      this.startCountdown();
      return;
    }
    
    // 计算剩余时间
    const hours = Math.floor(diff / (1000 * 60 * 60));
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((diff % (1000 * 60)) / 1000);
    
    // 更新数据
    this.setData({
      countdownHours: hours < 10 ? '0' + hours : '' + hours,
      countdownMinutes: minutes < 10 ? '0' + minutes : '' + minutes,
      countdownSeconds: seconds < 10 ? '0' + seconds : '' + seconds
    });
  },
  
  // 限时特购-查看更多
  onMoreTap: function() {
    wx.showToast({
      title: '更多商品功能开发中',
      icon: 'none'
    });
  },
  
  // 新品首发-查看更多
  onNewProductsMoreTap: function() {
    wx.showToast({
      title: '新品首发更多功能开发中',
      icon: 'none'
    });
  },
  
  // 商品点击事件
  onProductTap: function(e) {
    const id = e.currentTarget.dataset.id;
    // 跳转到商品详情页
    wx.navigateTo({
      url: `/pages/product/product?id=${id}`
    });
  },
  
  // 页面卸载时清除定时器
  onUnload: function() {
    if (this.countdownTimer) {
      clearInterval(this.countdownTimer);
    }
  }
})
